#import "YQPHomeFollowModel.h"

#import "MYHomeAttentionVC.h"
//#import "MYHomeAttentionUserCell.h"
#import "MYHomeAttentionContentCell.h"
#import "MYHomeAttentionTagCell.h"
#import "MYHomeAttentionImageCell.h"
#import "MYHomeAttentionToolCell.h"
#import "MYDynamicDetailsVC.h"
@interface MYHomeAttentionVC ()<MYBaseUserInfoCellDelegate>

@property (strong, nonatomic) NSMutableArray *followArray;

@property (strong, nonatomic) YQPHomeFollowModel *model;

@end

@implementation MYHomeAttentionVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.followArray = NSMutableArray.new;

    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getHomeFollowListApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.followArray.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getHomeFollowListApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}

#pragma mark - 首页关注列表
- (void)getHomeFollowListApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);

    [self.followArray removeAllObjects];
    [YQPNetWorkingManager sendGetRequest:kGET_INVITATION_FOLLOW_LIST_MANAGER_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQPHomeFollowModel *followModel = [YQPHomeFollowModel mj_objectWithKeyValues:responseObject.data];
        [self.followArray addObjectsFromArray:followModel.list];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-Height_TabBar-Height_NavBar);
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWCELL([MYHomeAttentionUserInfoCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionContentCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionTagCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionImageCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYHomeAttentionToolCell")
    kYQREGISTERTABLEVIEWCHeaderFooter([UITableViewHeaderFooterView class])
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.listTableView;
}


#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.followArray.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPFollowModel *model = self.followArray[indexPath.section];
    if (indexPath.row == 0) {
        MYHomeAttentionUserInfoCell *userCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionUserInfoCell"];
        userCell.indexPath = indexPath;
        userCell.delegate = self;
        userCell.rightBtn.userInteractionEnabled = NO;
        userCell.nameLb.text = model.name;
        [userCell.userIcon sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:IMAGE_NAMED(@"")];
        return userCell;
    }else if (indexPath.row == 1){
        MYHomeAttentionContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionContentCell"];
        contentCell.contentLb.text = model.title;//@"北京市经济和信息化局：2022年运营商5G网络累计投资超300亿元";
        return contentCell;
    }else if (indexPath.row == 2) {
        MYHomeAttentionTagCell *tagCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionTagCell"];
        return tagCell;
    }else if(indexPath.row == 3) {
        MYHomeAttentionImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionImageCell"];
//        [imageCell.bigImageView sd_setImageWithURL:[NSURL URLWithString:model.coverImg] placeholderImage:IMAGE_NAMED(@"")];//image = YQIMG(@"Test_Rectangle_icon");
        imageCell.followModel = model;
        imageCell.carousel.autoScroll = NO;
        return imageCell;
    }else {
        MYHomeAttentionToolCell *toolCell = [tableView dequeueReusableCellWithIdentifier:@"MYHomeAttentionToolCell"];
        return toolCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 52;
    }else if (indexPath.row == 1) {
        return 42;
    }else if (indexPath.row == 2) {
        return 25;
    }else if (indexPath.row == 3) {
        return kScreenWidth;
    }
    return 42;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPFollowModel *model = self.followArray[indexPath.section];

    MYDynamicDetailsVC *detailsVC = [[MYDynamicDetailsVC alloc] init];
    detailsVC.channelId = @"0";
    detailsVC.invitationId = model.invitationId;
    detailsVC.updateListBlock = ^{
        [self getHomeFollowListApi:NO];
    };
    kYQPUSH(detailsVC);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewHeaderFooterView"];
        header.contentView.backgroundColor = UIColor.whiteColor;
        return header;

    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 20;
    }
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

#pragma makr - MYBaseUserInfoCellDelegate
- (void)moreActionIndexPath:(NSIndexPath *)indexPath {
    
    QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:@"" message:@"" preferredStyle:(QMUIAlertControllerStyleActionSheet)];

    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消关注") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"复制链接") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@...",kYQLS(@"分享")] style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"举报") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    alert.sheetButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:kSystemMedium(20),NSKernAttributeName:@(0)};
    
    [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"取消") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    alert.sheetCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:kSystemMedium(20),NSKernAttributeName:@(0)};
    [alert showWithAnimated:YES];
}
#pragma makr - MYBaseUserInfoCellDelegate end
@end
